package com.zmj.sy.mom.srv.aps.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.zmj.sy.mom.srv.aps.bean.entity.order.ApsDetailWeekday;
import com.zmj.sy.mom.srv.aps.bean.vo.apsorder.ApsOrderGroupLoadResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.order.OrderGroupLoadsDetailResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.order.OrderProdGanttArrayResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.order.OrderWorkLoadsDataResVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface ApsDetailWeekdayMapper extends SyMapper<ApsDetailWeekday> {

    @Select("delete from tb_aps_detail_weekday where order_id = #{id}")
    void deleteOrderId(@Param("id") Integer id);

    @Select("select sg.group_id groupId, g.name groupName, g.my_code groupCode , adw.shift_day, sum(adw.work_time) usedWorkTime " +
            "from tb_aps_detail_weekday adw " +
            "left join tb_group g on adw.group_id = g.id " +
            "${ew.customSqlSegment}" )
    List<ApsOrderGroupLoadResVo> selectGroupLoad(@Param(Constants.WRAPPER) QueryWrapper<ApsDetailWeekday> query);

    @Select("select adw.shift_day, adw.group_id, adw.order_id, sum(adw.work_time) work_time " +
            "from tb_aps_detail_weekday adw " +
            "${ew.customSqlSegment} ")
    List<OrderProdGanttArrayResVo> selectProdGantt(@Param(Constants.WRAPPER) QueryWrapper<ApsDetailWeekday> wrapper);

    @Select("select s.id stationId, s.name stationName, s.my_code stationCode, wd.mater_no materCode, wd.mater_name materName," +
            "ao.seq seq, ao.order_code orderCode, adw.phase_code, adw.phase_name ," +
            "adw.plan_start_date_time, adw.plan_end_date_time " +
            "from tb_aps_detail_weekday adw " +
            "left join tb_aps_order ao on ao.id = adw.aps_order_id " +
            "left join tb_station s on adw.station_id = s.id  " +
            "left join tb_aps_work_detail wd on wd.id = adw.aps_work_detail_id " +
            "${ew.customSqlSegment} ")
    List<OrderGroupLoadsDetailResVo> selectAll(@Param(Constants.WRAPPER) QueryWrapper<ApsDetailWeekday> query);

    @Select(" select shift_day day, station_id stationId, sum(work_time) planMinutes " +
            " from tb_aps_detail_weekday dw " +
            " ${ew.customSqlSegment}")
    List<OrderWorkLoadsDataResVo> groupByStationSumTotal(@Param(Constants.WRAPPER) QueryWrapper<ApsDetailWeekday> query);

    @Select("delete from tb_aps_detail_weekday ${ew.customSqlSegment}")
    void deleteByApsWorkDetailId(@Param(Constants.WRAPPER) QueryWrapper<ApsDetailWeekday> query);

    @Select("delete from tb_aps_detail_weekday where phase_id = #{id} ")
    void deleteByPhaseId(@Param("id")Integer id);
}
